$(document).ready(function(){
	//登录密码
	var updatePwdLogin = new Helper.RemoteQuery({
		url:Helper.basePath + "/member_operate/goMemberPage/sub$sub_password_update_login.htm",
		renderId: "loaddiv",
		onSuccess:function(){
			commInit(1);
			loginPwdInit();
		}
	});
	//支付密码
	var updatePwdPayment = new Helper.RemoteQuery({
		url:Helper.basePath + "/member_operate/goUpdatePayPwd.htm",
		renderId: "loaddiv",
		onSuccess:function(){
			commInit(2);
			payPwdInit();
		}
	});
	
	/**
	 * 支付密码设置弹出款
	 */
	var show_setup_password_window = function(success)
	{
		var txpass_cont = new Helper.Window({
			id:"setup_draw_password_div",
			params : {winId : 'setup_draw_password_div'},
			url : Helper.basePath + "/page/memberinfo/user/sub/sub_password_set_payment.jsp",
			title : "您必须先设置支付密码",
			width : 436,
			height : 272,
			onSuccess: function(){
				this.close();
				if($.isFunction(success))
				{
					success();
				}
				else
				{
					window.location.reload(true);
				}
			}
		});
		txpass_cont.show();
	};
	
	/**
	 * flag:为true时表示输入正确，为false时表示提示信息
	 */
	var prompt = function(text, message,flag) {
		if(flag) {
			$(text).nextAll("span:last").html("<b class='aqxx_bg duihao_ico mr_2'></b>");
		}else {
			$(text).nextAll("span:last").html(message).removeClass("red").addClass("gray");
		}
	};
	var promptFailure = function(text, message) {
		$(text).nextAll("span:last").html("<b class='aqxx_bg gantan_ico mr_2'></b>"+message).removeClass("gray").addClass("red");
	};
	
	/**
	 * 失去焦点时密码检查
	 */
	var checkPwdBlur = function(name) {
		var value = $("#I"+name).val();
		var msg = Helper.validata.validataPass(value);
		if(undefined != msg){
			promptFailure($("#I"+name)[0], msg);
			return;
		}
		prompt($("#I"+name), "", true);
		return true;
	};
	
	/**
	 * 获得焦点时密码检查
	 */
	var checkPwdFocus = function(name, msg){
		prompt($("#I"+name), msg, false);
	};
	

	/**
	 * 新密码是否和旧密码相同
	 */
	var notEqNewAndOldPwd = function(){
		var IoldPwd = $("#IoldPwd").val();
		var InewPwd = $("#InewPwd").val();
		if(IoldPwd === InewPwd) {
			promptFailure($("#InewPwd")[0], "旧密码和新密码不能相同");
			return;
		}
		return true;
	};
	
	/**
	 * 检查重复密码
	 */
	var checkReNewPwd = function(){
		var InewPwd = $("#InewPwd").val();
		var IreNewPwd = $("#IreNewPwd").val();
		if(IreNewPwd != InewPwd) {
			promptFailure($("#IreNewPwd")[0], "重复密码和新密码不一致");
			return;
		}
		return true;
	};
	
	/**
	 * 密码框初始化
	 */
	var commInit = function(type) {
		/**
		 * 旧密码
		 */
		$("#IoldPwd").blur(function(){
			checkPwdBlur("oldPwd");
		});
		$("#IoldPwd").focus(function(){
			if(type == 1) {
				checkPwdFocus("oldPwd", "请输入登录密码");
			}else if(type == 2) {
				checkPwdFocus("oldPwd", "请输入支付密码");
			}
		});
		
		/**
		 * 新密码
		 */
		$("#InewPwd").blur(function(){
			if(checkPwdBlur("newPwd"))
			{
				notEqNewAndOldPwd();
			}
		});
		$("#InewPwd").focus(function(){
			checkPwdFocus("newPwd", "6-16个字符，可包含字母、数字、大小写混合");
		});
		
		/**
		 * 重复密码
		 */
		$("#IreNewPwd").blur(function(){
			if(checkPwdBlur("reNewPwd"))
			{
				checkReNewPwd();
			}
		});
		$("#IreNewPwd").focus(function(){
			checkPwdFocus("reNewPwd", "请输入重复密码");
		});
		
		if(type == 2 && $("#IisSet").val() == 0)
		{
			$("#InewPwd").focus();
		}
		else
		{
			$("#IoldPwd").focus();
		}
	};
	
	
	/**
	 * 登录密码初始化
	 * @returns
	 */
	var loginPwdInit = function() {
		/**
		 * 修改登录密码
		 */
		var updteLoginPwd = new Helper.RemoteTrans( {
			url: Helper.basePath + "/member_operate/doUpdateLoginPwd.htm",
			onSuccess: function(result){
				Helper.confirm("恭喜您，修改成功！");
				var returnUrl = $("#returnUrl").val();
				if(returnUrl){
					location.href = returnUrl;
				}else {
					loadLoginPwd.send();
				}
			},
			onError: function(result, status){
				Helper.message((result.message==null?"修改失败":result.message));
			}
		});
		
		/**
		 * 确认修改
		 */
		$("#loginPwdUpdate").click(function(){
			if(checkPwdBlur("oldPwd") && checkPwdBlur("newPwd") 
				&& notEqNewAndOldPwd() && checkReNewPwd()){
				updteLoginPwd.addParameters("oldPwd", $("#IoldPwd").val());
				updteLoginPwd.addParameters("newPwd", $("#InewPwd").val());
				updteLoginPwd.send();
			}
		});
	};
	
	/**
	 * 支付密码初始化
	 */
	var payPwdInit = function() {
		var isSetPwd = $("#IisSet").val();
		/**
		 * 修改支付密码
		 */
		var updtePayPwd = new Helper.RemoteTrans( {
			url: Helper.basePath + "/member_operate/doUpdatePayPwd.htm",
			onSuccess: function(result){
				Helper.message("恭喜您，"+(isSetPwd===1?"修改":"设置")+"成功！");
				var returnUrl = $("#returnUrl").val();
				if(returnUrl){
					location.href = returnUrl;
				}else {
					loadPayPwd.send();
				}
			},
			onError: function(result, status){
				Helper.message((result.message==null?(isSetPwd===1?"修改":"设置")+"失败":result.message));
			}
		});
		
		/**
		 * 确认修改
		 */
		var IisSet = $("#IisSet").val();
		$("#payPwdUpdate").click(function(){
			if(IisSet == 1) {
				if(checkPwdBlur("oldPwd") && checkPwdBlur("newPwd") 
					&& notEqNewAndOldPwd() && checkReNewPwd()){
					updtePayPwd.addParameters("oldPwd", $("#IoldPwd").val());
					updtePayPwd.addParameters("newPwd", $("#InewPwd").val());
					updtePayPwd.send();
				}
			}else {
				if(checkPwdBlur("newPwd") && checkReNewPwd()){
					updtePayPwd.addParameters("oldPwd", $("#IoldPwd").val());
					updtePayPwd.addParameters("newPwd", $("#InewPwd").val());
					updtePayPwd.send();
				}
			}
		});
	};
	
	//标签切换
	var currentTabIndex;
	var linkTabVal = $("#linkTabVal").val();
	var linkTabs_a = $("#updatePwdLinkTab>li>a");
	linkTabs_a.each(function(index){
		var lang = $(this).attr("lang");
		if(linkTabVal.toUpperCase() != lang.toUpperCase())
		{
			$(this).click(function(){
				if(currentTabIndex != index)
				{
					location.href = Helper.basePath+"/member_operate/goUpdatePwd"+lang+".htm";
				}
			});
		}
		else
		{ 
			currentTabIndex = index;
		}
		//修改密码页默认显示控制
		if(index == linkTabs_a.length-1)
		{
			currentTabIndex = currentTabIndex || 0;
			$(linkTabs_a[currentTabIndex]).parent("li").siblings("li").removeClass();
			$(linkTabs_a[currentTabIndex]).parent("li").attr("class", "gezx_dqan yellow");
			eval("updatePwd"+$(linkTabs_a[currentTabIndex]).attr("lang")+".send();");
		}
	});
});